package au.com.uptick.serendipity.client.presenter;

import au.com.uptick.serendipity.client.NameTokens;
import au.com.uptick.serendipity.client.Serendipity;
import au.com.uptick.serendipity.client.view.handlers.MainPageUiHandlers;
import au.com.uptick.serendipity.client.widgets.NavigationPane;
import au.com.uptick.serendipity.client.widgets.NavigationPaneHeader;
import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.event.shared.GwtEvent.Type;
import com.google.inject.Inject;
import com.gwtplatform.mvp.client.HasUiHandlers;
import com.gwtplatform.mvp.client.Presenter;
import com.gwtplatform.mvp.client.View;
import com.gwtplatform.mvp.client.annotations.ContentSlot;
import com.gwtplatform.mvp.client.annotations.NameToken;
import com.gwtplatform.mvp.client.annotations.ProxyCodeSplit;
import com.gwtplatform.mvp.client.proxy.*;

public class MainPagePresenter extends Presenter<MainPagePresenter.MyView, MainPagePresenter.MyProxy> implements MainPageUiHandlers {

    private final PlaceManager placeManager;

    private static NavigationPaneHeader navigationPaneHeader;
    private static NavigationPane navigationPane;

    @ProxyCodeSplit
    @NameToken(NameTokens.mainPage)
    public interface MyProxy extends Proxy<MainPagePresenter>, Place {
    }

    public interface MyView extends View, HasUiHandlers<MainPageUiHandlers> {
        NavigationPaneHeader getNavigationPaneHeader();

        NavigationPane getNavigationPane();
    }

    /**
     * Use this in leaf presenters, inside their {@link #revealInParent} method.
     */
    @ContentSlot
    public static final Type<RevealContentHandler<?>> TYPE_SetContextAreaContent = new Type<RevealContentHandler<?>>();

    @Inject
    public MainPagePresenter(EventBus eventBus, MyView view, MyProxy proxy,
                             PlaceManager placeManager) {
        super(eventBus, view, proxy);

        getView().setUiHandlers(this);

        this.placeManager = placeManager;

        MainPagePresenter.navigationPaneHeader = view.getNavigationPaneHeader();
        MainPagePresenter.navigationPane = view.getNavigationPane();
    }

    @Override
    protected void onBind() {
        super.onBind();

        Log.debug("onBind()");

        // expand the first Navigation Pane section
        getView().getNavigationPane().expandSection(Serendipity.getConstants().SalesStackSectionName());

        // reveal the first nested Presenter
        PlaceRequest placRequest = new PlaceRequest(NameTokens.activities);
        placeManager.revealPlace(placRequest);
    }

    @Override
    protected void onReveal() {
        super.onReveal();

        Log.debug("onReveal()");
    }

    @Override
    protected void onReset() {
        super.onReset();

        Log.debug("onReset()");
    }

    @Override
    protected void revealInParent() {
        // RevealRootLayoutContentEvent.fire(this, this);
        RevealRootContentEvent.fire(this, this);
    }

    public void onNavigationPaneSectionHeaderClicked(String name) {
        if (name.length() != 0) {
            PlaceRequest placeRequest = new PlaceRequest(name);
            placeManager.revealPlace(placeRequest);
        }
    }

    public void onNavigationPaneSectionClicked(String name) {
        if (name.length() != 0) {
            PlaceRequest placeRequest = new PlaceRequest(name);
            placeManager.revealPlace(placeRequest);
        }
    }

    public static NavigationPaneHeader getNavigationPaneHeader() {
        return navigationPaneHeader;
    }

    public static NavigationPane getNavigationPane() {
        return navigationPane;
    }
}
